package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.demo.entity.UserBean;
import com.example.demo.entity.dto.UserLoginDTO;
import com.example.demo.entity.vo.RegisterByPhoneVO;
import com.example.demo.entity.vo.UserLoginByPassVO;
import com.example.demo.result.Result;
import com.example.demo.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/*
 *
 *   @Author:Sjy
 *   @Date: 2024/3/4-03-04-19:36
 *
 */
//@Controller + @ResponseBody = @RestController
@Api(tags = "登录接口")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private IUserLoginService iUserLoginService;


    @GetMapping("/hello")
    public Result getMessage(){

        /*UserBean bean = null;
        bean.setAge(1);*/

        //默认返回页面地址 @ResponseBody 返回才是内容
        return Result.success(321);
    }

    @ApiOperation(value = "用户名密码登录")
    @PostMapping("/bypass")
    public Result<UserLoginDTO> userLoginByPass(@Valid @RequestBody UserLoginByPassVO userLoginByPassVO){
        UserBean bean = new UserBean();
        //vo转换bean
        BeanUtil.copyProperties(userLoginByPassVO,bean);
        //业务逻辑层
        UserLoginDTO userLoginDTO = iUserLoginService.userLoginByPass(bean);
        if(userLoginDTO ==null){
            return Result.error("用户名或密码错误").code(86203);
        }
        return Result.success(userLoginDTO);
    }
    
    
    @ApiOperation(value = "获取验证码")
    @PostMapping("/getVerificationCode/{phoneNumber}")
    public Result getVerificationCode(@PathVariable String phoneNumber){
        //生成6位随机验证码
        int c = RandomUtil.randomInt(100000, 999999);



        System.out.println("phoneNumber = " + phoneNumber);
        return Result.success();
    }


    //邮箱验证码注册
    @ApiOperation(value = "手机号注册")
    @PostMapping("/registerByPhone")
    public Result registerByPhone(@Valid @RequestBody RegisterByPhoneVO registerByPhoneVO){
        UserBean userBean = new UserBean();
        //复制对象的字段vo 给 bean
        BeanUtil.copyProperties(registerByPhoneVO,userBean);
        //不用判断返回结果，默认就是成功
        iUserLoginService.userRegisterByPhone(userBean);
        return Result.success().msg("用户注册成功");
    }


}
